package com.ip.student.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ip.common.constant.Result;
import com.ip.common.enums.CodeEnum;
import com.ip.common.utils.StrUtils;
import com.ip.user.models.common.annotation.RequiresPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ip.student.models.service.IStudentAwardService;
import com.ip.student.models.bo.StudentAwardSaveBO;
import com.ip.student.models.bo.StudentAwardBO;
import com.ip.student.models.dto.StudentAwardDTO;

/**
 * 奖励信息控制器
 *
 * @author 张景奇
 * @Date 2025-11-05 14:10:50
 */

@RestController
@RequestMapping("/cms/studentAward")
@Slf4j
@Api(value = "StudentAwardController",tags = "奖励信息接口")
public class StudentAwardController {

    @DubboReference
    private IStudentAwardService studentAwardService;

    @ApiOperation(value = "添加奖励信息",notes = "添加奖励信息")
    @PostMapping(value = "/insert")
    @RequiresPermission("student:award:add")
    public Result insert(@RequestBody StudentAwardSaveBO saveBO) {
        boolean flag = studentAwardService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存奖励信息",notes = "保存奖励信息")
    @RequiresPermission("student:award:add")
    public Result save(@RequestBody StudentAwardSaveBO saveBO){
        if(saveBO.getAwardId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改奖励信息",notes = "修改奖励信息")
    @PostMapping("/update")
    @RequiresPermission("student:award:update")
    public Result update(@RequestBody StudentAwardSaveBO saveBO){
        StudentAwardDTO dto = new StudentAwardDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setAwardId(saveBO.getAwardId());
        boolean flag = studentAwardService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除奖励信息",notes = "删除奖励信息")
    @ApiImplicitParam(name = "id",value = "奖励信息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    @RequiresPermission("student:award:delete")
    public Result delete(@RequestParam(value = "id", required = false) Long id, 
                         @RequestParam(value = "awardId", required = false) Long awardId,
                         @RequestBody(required = false) Map<String, Object> requestBody){
        Long deleteId = id != null ? id : awardId;
        
        // 如果通过请求参数没有获取到ID，则尝试从请求体中获取
        if (deleteId == null && requestBody != null && requestBody.containsKey("awardId")) {
            Object awardIdObj = requestBody.get("awardId");
            if (awardIdObj instanceof Number) {
                deleteId = ((Number) awardIdObj).longValue();
            } else if (awardIdObj instanceof String) {
                try {
                    deleteId = Long.parseLong((String) awardIdObj);
                } catch (NumberFormatException e) {
                    // 忽略解析错误
                }
            }
        }
        
        if (deleteId == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = studentAwardService.deleteById(deleteId.intValue());
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除奖励信息",notes = "批量删除奖励信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    @RequiresPermission("student:award:delete")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        studentAwardService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    @RequiresPermission("student:award:query")
    public Result<StudentAwardDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        StudentAwardDTO dto = studentAwardService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "奖励信息列表",notes = "奖励信息列表")
    @PostMapping("/queryPageList")
    @RequiresPermission("student:award:query")
    public Result<Page<StudentAwardDTO>> queryPageList(@RequestBody StudentAwardBO bo){
        Wrapper<StudentAwardDTO> wrapper = new EntityWrapper<>();
        // 根据学生ID进行查询
        if (bo.getStudentId() != null) {
            wrapper.eq("student_id", bo.getStudentId());
        }

        // 根据学年进行模糊查询
        if (StrUtils.isNotEmpty(bo.getSchoolYear())) {
            wrapper.like("school_year", bo.getSchoolYear());
        }

        // 根据学期进行模糊查询
        if (StrUtils.isNotEmpty(bo.getSemester())) {
            wrapper.like("semester", bo.getSemester());
        }
        //根据年级进行模糊查询
        if (StrUtils.isNotEmpty(bo.getGrade())) {
            wrapper.like("grade", bo.getGrade());
        }


        Page<StudentAwardDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = studentAwardService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}